#include <Python.h>

void garbage_initialize(void)
{	
	Py_Initialize();
	PyObject *sys = PyImport_ImportModule("sys");
	PyObject *path = PyObject_GetAttrString(sys,"path");
	PyList_Append(path,PyUnicode_FromString("."));
}

void garbage_final(void)
{	
	Py_Finalize();
}

char* garbage_category(char* category)
{
	PyObject *pModule = PyImport_ImportModule("garbage");
	if(!pModule)
	{
		PyErr_Print();
		printf("ERROR:failed to load nogarbage.py\n");
		goto FAILED_MODULE;
	}
	PyObject *pFunc = PyObject_GetAttrString(pModule,"alibabacloud_garbage");
	if(!pFunc || !PyCallable_Check(pFunc))
	{
		PyErr_Print();
		printf("ERROR: function say_funnay is not found or no callable\n");
		goto FAILED_VALUE;
	}
	PyObject *pValue = PyObject_CallObject(pFunc,NULL);
	
	if(!pValue)
	{
		PyErr_Print();
		printf("ERROR:function call failed \n");
		goto FAILED_VALUE;
		
	}
	char *result = NULL;
	if(!PyArg_Parse(pValue,"s",&result))
	{
		PyErr_Print();
		printf("ERROR:parse failed\n");
		goto FAILED_RESULT;
	}

	category = (char *)malloc( sizeof( char ) * (strlen(result)+ 1)); 
	memset(category,0,(strlen(result)+1));
	strncpy(category,result,(strlen(result) + 1));


FAILED_RESULT:
	Py_DECREF(pValue);
FAILED_VALUE:
	Py_DECREF(pFunc);
FAILED_FUNC:
	Py_DECREF(pModule);
FAILED_MODULE:
	return category;

}
